<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class business_field extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->library('common');
    }

    /*
     * This function is used to get the number of row in bussinessfield table
     * :v :v :3 (flex)
     */

    function total_count() {
        $query = $this->db->get('businessfield');

        return $query->num_rows();
    }

    /*
     * This function gets fields from businessfield table in database
     * $id: id of row is retrieved, if id=-1 then get a list
     * $limit: the number of row to retrieved
     * $offset: the offset to start retrieving
     * $limit and offset is only used when id =-1
     */

    function get($id = -1, $limit = -1, $offset = 0) {
        if ($id == -1) {
            if ($limit == -1) {
                $query = $this->db->get('businessfield');
            } else {
                $query = $this->db->get('businessfield', $limit, $offset);
            }

            $result = $query->result();
            return $result;
        } else {
            $this->db->where('id', $id);
            $query = $this->db->get('businessfield');
            
            if ($query->num_rows() > 0) {
                $result = $query->result();
                return $result[0];
            }
            return null;
        }
    }

    /*
     * This function is used to add a new record(row) to current table
     */

    function insert($name, $description, $content, $permalink, $image) {
        $field = array(
            'name' => $name,
            'description' => $description,
            'date' => date('Y/m/d'),
            'content' => $content,
            'permalink' => $permalink,
            'image' => $image);

        $this->db->insert('businessfield', $field);

        if ($this->db->affected_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function delete($id) {
        $this->db->delete('businessfield', array('id' => $id));
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function edit($id, $name, $description, $content, $permalink, $image) {
        $this->db->where('id', $id);
        $data = array(
            'name' => $name,
            'description' => $description,
            'date' => date('Y/m/d'),
            'content' => $content,
            'permalink' => $permalink,
            'image' => $image
        );
        $this->db->update('businessfield', $data);

        if ($this->db->affected_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * generate permalink for page
     * @param string $str name of a news
     * @param type $max_length max length of link
     * @return string permalink
     */
    function generate_permalink($str, $max_length = -1) {
        if ($max_length > 0) {
            if (strlen($str) > $max_length) {
                $link = $this->truncateString_($str, $max_length);
            } else
                $link = $str;
        }
        else {
            $link = $str;
        }

        $link = $this->common->removesign($str);
        $link = $this->common->remove_disallow($link);
        $link = $this->common->removespace($link);
        $link = urlencode($link);

        if ($this->check_permalink($link) == false) {
            return $link;
        }
        $i = 0;
        while (1) {
            $i++;
            $link_temp = $link . '-' . $i . '-';
            if ($this->check_permalink($link_temp) == false)
                return $link_temp;
        }
    }

    function get_by_permalink($str) {
        $query = $this->db->get_where('businessfield', array('permalink' => $str));
        $result = $query->result();
        if (count($result) > 0) {
            return $result[0];
        } else {
            return null;
        }
    }

    function check_permalink($str) {
        $news = $this->get_by_permalink($str);
        if (isset($news) && $news != NULL) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}
?>

